﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler
{
    public class DelURCommandHandler : IRequestHandler<DelURCommand, APIResult<object>>
    {
        private readonly IBaseRepository<RoleUser> urRep;
        public DelURCommandHandler(IBaseRepository<RoleUser> urRep)
        {
            this.urRep = urRep;
        }

        /// <summary>
        /// 删除角色用户关联
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<object>> Handle(DelURCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var obj =request.RUId.Select(p=>new RoleUser { RUId=p}).ToList();
           var res= urRep.DelList(obj).Result;
          
            if (res > 0)
            {

                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }

            return Task.FromResult(result);
        }
    }
}
